package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.BarDataBean;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.EquipRepairTotal;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.BarChartUtil;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil03;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by z on 2018/9/17.
 * Des:机电故障汇总Activity
 */

public class ElectricalAndMechanicalFailureActivity extends BaseActivity {
    @BindView(R.id.mSpinnerDanwei)
    MySpinner mSpinnerDanwei;
    @BindView(R.id.mSpinnerTime)
    TextView mSpinnerTime;
    @BindView(R.id.mBarChart)
    BarChart mBarChart;
    @BindView(R.id.iv_back2)
    ImageView ivBack2;
    @BindView(R.id.tv_left_title)
    TextView tvLeftTitle;
    @BindView(R.id.iv_center_title)
    ImageView ivCenterTitle;
    @BindView(R.id.iv_end_title)
    ImageView ivEndTitle;
    @BindView(R.id.tv_end_title)
    TextView tvEndTitle;
    @BindView(R.id.iv_right_title)
    ImageView ivRightTitle;
    @BindView(R.id.callVideo)
    ImageView callVideo;
    @BindView(R.id.titleAddIcon)
    ImageView titleAddIcon;
    @BindView(R.id.app_title_rela)
    RelativeLayout appTitleRela;
    @BindView(R.id.shoufeiText)
    TextView shoufeiText;
    @BindView(R.id.jiankongText)
    TextView jiankongText;
    @BindView(R.id.gongdiannText)
    TextView gongdiannText;
    @BindView(R.id.zhaomingText)
    TextView zhaomingText;
    @BindView(R.id.tongfenText)
    TextView tongfenText;
    @BindView(R.id.yxianluText)
    TextView yxianluText;
    @BindView(R.id.ygongdianText)
    TextView ygongdianText;
    @BindView(R.id.yyingjianText)
    TextView yyingjianText;
    @BindView(R.id.yruanjianText)
    TextView yruanjianText;
    @BindView(R.id.wgenghuanText)
    TextView wgenghuanText;
    @BindView(R.id.wxianluText)
    TextView wxianluText;
    @BindView(R.id.wtiaoshiText)
    TextView wtiaoshiText;
    @BindView(R.id.wqitaText)
    TextView wqitaText;
    @BindView(R.id.jzhengchangText)
    TextView jzhengchangText;
    @BindView(R.id.jfanxiuText)
    TextView jfanxiuText;
    @BindView(R.id.jqitaText)
    TextView jqitaText;
    @BindView(R.id.ljiankongText)
    TextView ljiankongText;
    @BindView(R.id.linearLayout01)
    LinearLayout linearLayout01;
    @BindView(R.id.linearLayout02)
    LinearLayout linearLayout02;
    @BindView(R.id.linearLayout03)
    LinearLayout linearLayout03;
    @BindView(R.id.linearLayout04)
    LinearLayout linearLayout04;


    private OrgListBean orgListBean;
    private int selectPoastion;
    private EquipRepairTotal equipRepairTotal;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_electrical_mechanical_failure_layout;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setVisibility(View.VISIBLE);
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(R.string.jidianguzhang);
    }

    @Override
    public void initData() {
        super.initData();
        mSpinnerTime.setText(TimeUtils.dateStringYM(System.currentTimeMillis()));

        mSpinnerTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerUtil03.getInstance().show(ElectricalAndMechanicalFailureActivity.this, new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
                        Log.e("时间数据", new Gson().toJson(dateBean));
                        mSpinnerTime.setText(dateBean.getmDate());
                        getHttpData(orgListBean.getList().get(selectPoastion).getId());
                    }
                });
            }
        });

        getHttpDataJiGou();
    }

    @Override
    public void setListener() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 获取机构
     */
    private void getHttpDataJiGou() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                mSpinnerDanwei.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        selectPoastion = postion;
                        getHttpData(orgListBean.getList().get(postion).getId());
                    }
                });
                getHttpData(orgListBean.getList().get(0).getId());
            }
        });
    }

    /**
     * 获取shuju
     */
    private void getHttpData(String orgId) {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("orgId", orgId);
        hashMap.put("period", mSpinnerTime.getText().toString());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getEquipRepairTotal);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(String code, String response) {
                equipRepairTotal = new Gson().fromJson(response, EquipRepairTotal.class);
                if (equipRepairTotal.getList() == null || equipRepairTotal.getList().size() == 0) {
                    ToastUtils.showToast("暂无数据");
                    linearLayout01.setOnClickListener(null);
                    linearLayout02.setOnClickListener(null);
                    linearLayout03.setOnClickListener(null);
                    linearLayout04.setOnClickListener(null);
                    return;
                }
                BarChartUtil.setBarChartchange(mBarChart, getBarDataBeans01(), getColors(), "");
                linearLayout01.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BarChartUtil.setBarChart(mBarChart, getBarDataBeans01(), getColors(), "");
                        initbackground();
                        linearLayout01.setBackgroundColor(getResources().getColor(R.color.gray));
                    }

                });
                linearLayout02.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BarChartUtil.setBarChart(mBarChart, getBarDataBeans02(), getColors(), "");
                        initbackground();
                        linearLayout02.setBackgroundColor(getResources().getColor(R.color.gray));
                    }
                });
                linearLayout03.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BarChartUtil.setBarChart(mBarChart, getBarDataBeans03(), getColors(), "");
                        initbackground();
                        linearLayout03.setBackgroundColor(getResources().getColor(R.color.gray));
                    }
                });
                linearLayout04.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BarChartUtil.setBarChart(mBarChart, getBarDataBeans04(), getColors(), "");
                        initbackground();
                        linearLayout04.setBackgroundColor(getResources().getColor(R.color.gray));
                    }
                });
                shoufeiText.setText("收费:【" + equipRepairTotal.getList().get(0).getLshoufei() + "处】");
                jiankongText.setText("通信:【" + equipRepairTotal.getList().get(0).getLtongxin() + "处】");
                gongdiannText.setText("供电:【" + equipRepairTotal.getList().get(0).getLgongdian() + "处】");
                zhaomingText.setText("照明:【" + equipRepairTotal.getList().get(0).getLzhaoming() + "处】");
                tongfenText.setText("通风:【" + equipRepairTotal.getList().get(0).getLtongfeng() + "处】");
                ljiankongText.setText("监控:【" + equipRepairTotal.getList().get(0).getLjiankong() + "处】");
                yxianluText.setText("线路:【" + equipRepairTotal.getList().get(0).getYxianlu() + "处】");
                ygongdianText.setText("供电:【" + equipRepairTotal.getList().get(0).getYgongdian() + "处】");
                yyingjianText.setText("硬件:【" + equipRepairTotal.getList().get(0).getYyingjian() + "处】");
                yruanjianText.setText("软件:【" + equipRepairTotal.getList().get(0).getYruanjian() + "处】");
                wgenghuanText.setText("更换:【" + equipRepairTotal.getList().get(0).getWgenghuan() + "处】");
                wxianluText.setText("线路:【" + equipRepairTotal.getList().get(0).getWxianlu() + "处】");
                wtiaoshiText.setText("调试:【" + equipRepairTotal.getList().get(0).getWtiaoshi() + "处】");
                wqitaText.setText("其它:【" + equipRepairTotal.getList().get(0).getWqita() + "处】");
                jzhengchangText.setText("正常:【" + equipRepairTotal.getList().get(0).getJzhengchang() + "处】");
                jfanxiuText.setText("返修:【" + equipRepairTotal.getList().get(0).getJfanxiu() + "处】");
                jqitaText.setText("其它:【" + equipRepairTotal.getList().get(0).getJqita() + "处】");
            }
        });
    }

    private void initbackground() {
        linearLayout01.setBackgroundColor(getResources().getColor(R.color.white));
        linearLayout02.setBackgroundColor(getResources().getColor(R.color.white));
        linearLayout03.setBackgroundColor(getResources().getColor(R.color.white));
        linearLayout04.setBackgroundColor(getResources().getColor(R.color.white));
    }

    private int[] getColors() {
        int[] colors = new int[7];
        colors[0] = getResources().getColor(R.color.bar_color01);
        colors[1] = getResources().getColor(R.color.bar_color02);
        colors[2] = getResources().getColor(R.color.bar_color03);
        colors[3] = getResources().getColor(R.color.bar_color04);
        colors[4] = getResources().getColor(R.color.bar_color05);
        colors[5] = getResources().getColor(R.color.bar_color06);
        colors[6] = getResources().getColor(R.color.bar_color07);
        return colors;
    }

    private List<BarDataBean> getBarDataBeans01() {
        List<BarDataBean> barDataBeans = new ArrayList<>();
        BarDataBean barDataBean = new BarDataBean();
        barDataBean.setxName("");
        barDataBean.setfValue(0);
        barDataBeans.add(barDataBean);

        BarDataBean barDataBean01 = new BarDataBean();
        barDataBean01.setxName("收费");
        barDataBean01.setfValue(equipRepairTotal.getList().get(0).getLshoufei());
        barDataBeans.add(barDataBean01);

        BarDataBean barDataBean02 = new BarDataBean();
        barDataBean02.setxName("通信");
        barDataBean02.setfValue(equipRepairTotal.getList().get(0).getLtongxin());
        barDataBeans.add(barDataBean02);

        BarDataBean barDataBean03 = new BarDataBean();
        barDataBean03.setxName("供电");
        barDataBean03.setfValue(equipRepairTotal.getList().get(0).getLgongdian());
        barDataBeans.add(barDataBean03);

        BarDataBean barDataBean04 = new BarDataBean();
        barDataBean04.setxName("照明");
        barDataBean04.setfValue(equipRepairTotal.getList().get(0).getLzhaoming());
        barDataBeans.add(barDataBean04);

        BarDataBean barDataBean05 = new BarDataBean();
        barDataBean05.setxName("通风");
        barDataBean05.setfValue(equipRepairTotal.getList().get(0).getLtongfeng());
        barDataBeans.add(barDataBean05);

        BarDataBean barDataBean06 = new BarDataBean();
        barDataBean06.setxName("监控");
        barDataBean06.setfValue(equipRepairTotal.getList().get(0).getLjiankong());
        barDataBeans.add(barDataBean06);

        return barDataBeans;
    }

    private List<BarDataBean> getBarDataBeans02() {
        List<BarDataBean> barDataBeans = new ArrayList<>();

        BarDataBean barDataBean = new BarDataBean();
        barDataBean.setxName("");
        barDataBean.setfValue(0);
        barDataBeans.add(barDataBean);

        BarDataBean barDataBean07 = new BarDataBean();
        barDataBean07.setxName("线路");
        barDataBean07.setfValue(equipRepairTotal.getList().get(0).getYxianlu());
        barDataBeans.add(barDataBean07);

        BarDataBean barDataBean08 = new BarDataBean();
        barDataBean08.setxName("供电");
        barDataBean08.setfValue(equipRepairTotal.getList().get(0).getYgongdian());
        barDataBeans.add(barDataBean08);

        BarDataBean barDataBean09 = new BarDataBean();
        barDataBean09.setxName("硬件");
        barDataBean09.setfValue(equipRepairTotal.getList().get(0).getYyingjian());
        barDataBeans.add(barDataBean09);

        BarDataBean barDataBean10 = new BarDataBean();
        barDataBean10.setxName("软件");
        barDataBean10.setfValue(equipRepairTotal.getList().get(0).getYruanjian());
        barDataBeans.add(barDataBean10);
        return barDataBeans;
    }

    private List<BarDataBean> getBarDataBeans03() {
        List<BarDataBean> barDataBeans = new ArrayList<>();

        BarDataBean barDataBean = new BarDataBean();
        barDataBean.setxName("");
        barDataBean.setfValue(0);
        barDataBeans.add(barDataBean);

        BarDataBean barDataBean11 = new BarDataBean();
        barDataBean11.setxName("更换");
        barDataBean11.setfValue(equipRepairTotal.getList().get(0).getWgenghuan());
        barDataBeans.add(barDataBean11);

        BarDataBean barDataBean12 = new BarDataBean();
        barDataBean12.setxName("线路");
        barDataBean12.setfValue(equipRepairTotal.getList().get(0).getWxianlu());
        barDataBeans.add(barDataBean12);

        BarDataBean barDataBean13 = new BarDataBean();
        barDataBean13.setxName("调试");
        barDataBean13.setfValue(equipRepairTotal.getList().get(0).getWtiaoshi());
        barDataBeans.add(barDataBean13);

        BarDataBean barDataBean14 = new BarDataBean();
        barDataBean14.setxName("其它");
        barDataBean14.setfValue(equipRepairTotal.getList().get(0).getWqita());
        barDataBeans.add(barDataBean14);
        return barDataBeans;
    }

    private List<BarDataBean> getBarDataBeans04() {
        List<BarDataBean> barDataBeans = new ArrayList<>();

        BarDataBean barDataBean = new BarDataBean();
        barDataBean.setxName("");
        barDataBean.setfValue(0);
        barDataBeans.add(barDataBean);

        BarDataBean barDataBean15 = new BarDataBean();
        barDataBean15.setxName("正常");
        barDataBean15.setfValue(equipRepairTotal.getList().get(0).getJzhengchang());
        barDataBeans.add(barDataBean15);

        BarDataBean barDataBean16 = new BarDataBean();
        barDataBean16.setxName("返修");
        barDataBean16.setfValue(equipRepairTotal.getList().get(0).getJfanxiu());
        barDataBeans.add(barDataBean16);

        BarDataBean barDataBean17 = new BarDataBean();
        barDataBean17.setxName("其它");
        barDataBean17.setfValue(equipRepairTotal.getList().get(0).getJqita());
        barDataBeans.add(barDataBean17);
        return barDataBeans;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
